package com.administrator.cloudapplication.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.administrator.cloudapplication.Interface.NearItemClick;
import com.administrator.cloudapplication.R;
import com.administrator.cloudapplication.constant.Urls;
import com.administrator.cloudapplication.models.NearLibraryModel;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/4/21.
 */

public class NearLibraryAdapter extends BaseAdapter {
    Context mContext;
    LayoutInflater mInflater;
    private NearItemClick callback;
    ArrayList<NearLibraryModel.DataBean> mListType = new ArrayList<>();
    public NearLibraryAdapter(Activity activity, ArrayList<NearLibraryModel.DataBean> mDataBean,
                              NearItemClick callback){
        mListType = mDataBean;
        mContext = activity;
       mInflater = LayoutInflater.from(activity);
        this.callback=callback;
    }

    @Override
    public int getCount() {
        return mListType.size();
    }

    @Override
    public Object getItem(int i) {
        return mListType.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    private class ViewHolder {
        ImageView img;
        TextView tv_top;
        TextView tv_phone;
        TextView tv_address;
        LinearLayout ll_near_item;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.near_library_item, null);
            holder = new ViewHolder();
            holder.img = (ImageView) view.findViewById(R.id.img);
            holder.tv_top = (TextView) view.findViewById(R.id.tv_top);
            holder.tv_phone = (TextView) view.findViewById(R.id.tv_phone);
            holder.tv_address = (TextView) view.findViewById(R.id.tv_address);
            holder.ll_near_item= (LinearLayout) view.findViewById(R.id.ll_near_item);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        final NearLibraryModel.DataBean dataBean= (NearLibraryModel.DataBean) getItem(i);
        String str= Urls.IMG_URL + dataBean.getThumb();
        Glide.with(mContext).load(str).placeholder(R.mipmap.error).error(R.mipmap.error).into(holder.img);
        holder.tv_top.setText(dataBean.getName());
        holder.tv_phone.setText("电话："+dataBean.getTel());
        holder.tv_address.setText(dataBean.getAddress());
        holder.ll_near_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callback.onClick(dataBean);
            }
        });
        return view;
    }
}
